package dev;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

@SuppressWarnings("serial")
public class Sending_Mail extends HttpServlet {
	@SuppressWarnings("deprecation")
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, ServletException {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
		Calendar date=new GregorianCalendar();
		date.add(Calendar.DATE, 1);
		String jour = sdf.format(date.getTime());
		if (jour.equals("Monday"))
			jour = "lundi";
		if (jour.equals("Tuesday"))
			jour = "mardi";
		if (jour.equals("Wednesday"))
			jour = "mercredi";
		if (jour.equals("Thursday"))
			jour = "jeudi";
		if (jour.equals("Friday"))
			jour = "vendredi";
		if (jour.equals("Saturday"))
			jour = "samedi";
		if (jour.equals("Sunday"))
			jour = "dimanche";
		
		
		Query q = new Query("Collecte");
		q.addFilter("bleu", Query.FilterOperator.EQUAL, jour);
		PreparedQuery pq = datastore.prepare(q);

		ArrayList<Entity> collectes_bleu = new ArrayList<Entity>();
		for (Entity result : pq.asIterable()) {
			collectes_bleu.add(result);
		}
		
		q = new Query("Collecte");
		q.addFilter("jaune", Query.FilterOperator.EQUAL, jour);
		pq = datastore.prepare(q);

		ArrayList<Entity> collectes_jaune = new ArrayList<Entity>();
		for (Entity result : pq.asIterable()) {
			collectes_jaune.add(result);
		}
		
		q = new Query("Collecte");
		q.addFilter("trisac1", Query.FilterOperator.EQUAL, jour);
		pq = datastore.prepare(q);

		ArrayList<Entity> collectes_tri1 = new ArrayList<Entity>();
		for (Entity result : pq.asIterable()) {
			collectes_tri1.add(result);
		}
		
		q = new Query("Collecte");
		q.addFilter("trisac2", Query.FilterOperator.EQUAL, jour);
		pq = datastore.prepare(q);

		ArrayList<Entity> collectes_tri2 = new ArrayList<Entity>();
		for (Entity result : pq.asIterable()) {
			collectes_tri2.add(result);
		}
		
		
		

		try {
			for (int i = 0; i < collectes_bleu.size(); i++) {
				Properties props = new Properties();
				Session session = Session.getDefaultInstance(props, null);
				Message msg = new MimeMessage(session);
				msg.setFrom(new InternetAddress("clean.nantes@gmail.com"));
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
						collectes_bleu.get(i).getProperty("user_email").toString()));
				msg.setSubject("Demain : Poubelles Bleues !");
				msg.setText("Demain n'oublies pas de sortir tes poubelles bleues =) !!!");
				Transport.send(msg);
			}
			
			for (int i = 0; i < collectes_jaune.size(); i++) {
				Properties props = new Properties();
				Session session = Session.getDefaultInstance(props, null);
				Message msg = new MimeMessage(session);
				msg.setFrom(new InternetAddress("clean.nantes@gmail.com"));
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
						collectes_jaune.get(i).getProperty("user_email").toString()));
				msg.setSubject("Demain : Poubelles Jaunes !");
				msg.setText("Demain n'oublies pas de sortir tes poubelles jaunes =) !!!");
				Transport.send(msg);
			}
			
			for (int i = 0; i < collectes_tri1.size(); i++) {
				Properties props = new Properties();
				Session session = Session.getDefaultInstance(props, null);
				Message msg = new MimeMessage(session);
				msg.setFrom(new InternetAddress("clean.nantes@gmail.com"));
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
						collectes_tri1.get(i).getProperty("user_email").toString()));
				msg.setSubject("Demain : Poubelles Tri'Sac !");
				msg.setText("Demain n'oublies pas de sortir tes poubelles tri'sac =) !!!");
				Transport.send(msg);
			}
			
			for (int i = 0; i < collectes_tri2.size(); i++) {
				Properties props = new Properties();
				Session session = Session.getDefaultInstance(props, null);
				Message msg = new MimeMessage(session);
				msg.setFrom(new InternetAddress("clean.nantes@gmail.com"));
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
						collectes_tri2.get(i).getProperty("user_email").toString()));
				msg.setSubject("Demain : Poubelles Tri'Sac !");
				msg.setText("Demain n'oublies pas de sortir tes poubelles tri'sac =) !!!");
				Transport.send(msg);
			}
		} catch (AddressException e) {
			// ...
		} catch (MessagingException e) {
			// ...
		}

	}
}